package mgz.androidutils.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import mgz.androidutils.R;

/**
 * Created by $Liudan on 2018/6/22 0022.
 */
public class TabBottomNavigation extends FrameLayout {
    private OnItemTabClickListener onItemTabClickListener;


    //当不需要使用xml声明或者不需要使用inflate动态加载时候，实现此构造函数即可
    public TabBottomNavigation(@NonNull Context context) {
        super(context);
        init();
    }

    //当需要在xml中声明此控件，则需要实现此构造函数。并且在构造函数中把自定义的属性与控件的数据成员连接起来。
    public TabBottomNavigation(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.tab_bottom_navigation, this);
        init();
    }

    //接受一个style资源
    public TabBottomNavigation(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.tab_bottom_navigation, this);
        init();
    }

    //可以直接获取传入资源ID，如R.style.purple
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TabBottomNavigation(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        LayoutInflater.from(context).inflate(R.layout.tab_bottom_navigation, this);
        init();
    }

    private void init(){
        LinearLayout tabNavigation = (LinearLayout)findViewById(R.id.tab_navigation);

        int tabCount = tabNavigation.getChildCount();

        for(int i=0;i<tabCount;i++){
            tabNavigation.getChildAt(i).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemTabClickListener!=null){
                        onItemTabClickListener.onItemTabClick(v);
                    }
                }
            });
        }
    }

    public void setItemTabOnClickListener(OnItemTabClickListener onItemTabClickListener){
        this.onItemTabClickListener = onItemTabClickListener;
    }

    public interface OnItemTabClickListener{
        void onItemTabClick(View view);
    }
}
